from django import forms
import re
from django.contrib.auth.models import User
from django.http import HttpRequest
from online_shop.captchas.fields import CaptchaField


class RegistrationForm(forms.Form):
    username = forms.CharField(label=u'Username', max_length=30)
    email = forms.EmailField(label=u'Email')
    password1 = forms.CharField(
        label=u'Password',
        widget=forms.PasswordInput()
    )
    password2 = forms.CharField(
        label=u'Password (Again)',
        widget=forms.PasswordInput()
    )
    captcha = CaptchaField()
    def clean_password2(self):
        if 'password1' in self.cleaned_data:
            password1 = self.cleaned_data['password1']
            password2 = self.cleaned_data['password2']
            if password1 == password2:
                return password2
        raise forms.ValidationError('Password do not match.')
    def clean_username(self):
        username = self.cleaned_data['username']
        if not re.search(r'^\w+$', username):
            raise forms.ValidationError('Username can only contain'
                                        'alphanumeric characters and the underscore.')
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError('Username is already taken.')

class PersonalInfo(forms.Form):
    email = forms.EmailField(label=u'Email')
    firstName = forms.RegexField(label=u'First Name', max_length=30, regex=r'^[A-Za-z]+$', error_message = ('First name can only contain letters.'), required=False)
    lastName = forms.RegexField(label=u'Last Name', max_length=30, regex=r'^[A-Za-z]+$', error_message = ('Last name can only contain letters.'), required=False)

class DeliverAddress(forms.Form):
    consignee = forms.RegexField(label=u'Consignee', max_length=30, regex=r'^[A-Za-z]+$', error_message = ('Consignee can only contain letters.'), widget=forms.TextInput(attrs={'size':'20'}))
    street_address = forms.CharField(
        label=u'Street Address',
        max_length=256,
        widget=forms.TextInput(attrs={'size':'40'})
    )
    postal_code = forms.RegexField(label=u'Postal Code', max_length=10, regex=r'^[0-9]+$', error_message = ('Postal code can only contain numbers.'),  widget=forms.TextInput(attrs={'size':'10'}))
    city = forms.CharField(
        label = u'City',
        max_length = 64,
        widget=forms.TextInput(attrs={'size':'10'})
    )
    state = forms.CharField(
        required = False,
        label = u'State',
        max_length = 64,
        widget=forms.TextInput(attrs={'size':'10'})
    )
    country = forms.CharField(
        label = u'Country',
        max_length = 64,
        widget=forms.TextInput(attrs={'size':'10'})
    )
    phone = forms.CharField(
        label = u'Mobile Phone',
        max_length = 20,
        widget=forms.TextInput(attrs={'size':'20'})
    )

class CommentForm(forms.Form):
    content = forms.CharField(
        label = u'Content',
        widget = forms.Textarea()
    )
                               